/*
 * addon_system.cpp
 *
 *  Created on: 2010-1-7
 *      Author: gavinkwoe
 */

#include "addon_features.h"

#if (__ADDON_TARGET__ == __TARGET_SYMBIAN)

#include "addon_std.h"
#include "addon_mem.h"
#include "addon_objbase.h"
#include "addon_tracer.h"

//#include <e32cmn.h>
//#include <e32debug.h>
//#include <string.h>
//#include <stdarg.h>
//#include <stdio.h>

/*************************************************************************
* GUID
*************************************************************************/

ADDON_DEFINED_GUID( IAddonTracer, 1, "E3CA954E-4EE8-4158-AF36-EAB4093965BE" )

/*************************************************************************
* STRUCTURE
*************************************************************************/

struct __struct_addon_Tracer_data;
typedef struct __struct_addon_Tracer_data		addon_Tracer_data_t;
typedef struct __struct_addon_Tracer_data *		addon_Tracer_data_ptr_t;

/*************************************************************************
* DEFINITION
*************************************************************************/

struct __struct_addon_Tracer_data
{
	unsigned long	ref_cnt;		// reference count
	unsigned char	trace_buff[512];
};

/*************************************************************************
* LOCAL FUNCTIONS
*************************************************************************/

unsigned long	__CTracer_Construct( ADDON_REF(IObject) thiz, void * usr_data );
unsigned long	__CTracer_Destruct( ADDON_REF(IObject) thiz );
unsigned long	__CTracer_AddRef( ADDON_REF(IObject) thiz );
unsigned long	__CTracer_Release( ADDON_REF(IObject) thiz );

unsigned long	__CTracer_Trace( ADDON_REF(IObject) thiz, char * fmt, ... );

// const void * const __coclass_vtbl[] =
// {
// 	// IObject
// 	(void *)__CTracer_Construct,
// 		(void *)__CTracer_Destruct,
// 		(void *)__CTracer_AddRef,
// 		(void *)__CTracer_Release,
// 		
// 		// IAddonSetting
// 		(void *)__CTracer_Trace
// };
// 
// const addon_coclass_desc_t __coclass_desc =
// {
// 	(unsigned char *)__coclass_vtbl,
// 		(unsigned long)sizeof( addon_Tracer_data_t )
// };

/*************************************************************************
* IMPLEMENTATION
*************************************************************************/

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CTracer_Construct( ADDON_REF(IObject) thiz, void * usr_data )
{
	addon_Tracer_data_ptr_t obj_data = (addon_Tracer_data_ptr_t)ADDON_GET_DATA( thiz );

	ADDON_UNUSED( usr_data );
	
	if ( NULL == obj_data )
		return ADDON_FAILED;

	obj_data->ref_cnt = 1;

	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CTracer_Destruct( ADDON_REF(IObject) thiz )
{
	addon_Tracer_data_ptr_t obj_data = (addon_Tracer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CTracer_AddRef( ADDON_REF(IObject) thiz )
{
	addon_Tracer_data_ptr_t obj_data = (addon_Tracer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	obj_data->ref_cnt++;
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CTracer_Release( ADDON_REF(IObject) thiz )
{
	addon_Tracer_data_ptr_t obj_data = (addon_Tracer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	obj_data->ref_cnt -= 1;

	if ( 0 == obj_data->ref_cnt )
	{
		addon_obj_delete( (void *)thiz );
	}
	
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CTracer_Trace( ADDON_REF(IObject) thiz, char * fmt, ... )
{
//	va_list					ap;
//	addon_Tracer_data_ptr_t obj_data = (addon_Tracer_data_ptr_t)ADDON_GET_DATA( thiz );
//	
//	if ( NULL == obj_data || NULL == fmt )
//		return ADDON_FAILED;
//	
//	va_start( ap, fmt );
//
//	obj_data->trace_buff[0] = '\0';
//	vsprintf( (char *)obj_data->trace_buff, (const char *)fmt, ap );
//
//	RDebug::Printf( (char *)obj_data->trace_buff );
//
//	va_end( ap );

	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
ADDON_EXPORT ADDON_REF(IObject) addon_create_tracer( void * reserved )
{
	//static const 
		void * __coclass_vtbl[5];// =
	int i=0;//{
		// IObject
__coclass_vtbl[i++] = 			(void *)__CTracer_Construct;
__coclass_vtbl[i++] = 	(void *)__CTracer_Destruct;
__coclass_vtbl[i++] = 	(void *)__CTracer_AddRef;
__coclass_vtbl[i++] = 	(void *)__CTracer_Release;

		// IAddonSetting
__coclass_vtbl[i++] = 	(void *)__CTracer_Trace;
	//};

// 	static const addon_coclass_desc_t __coclass_desc =
// 	{
// 		(unsigned char *)__coclass_vtbl,
// 		(unsigned long)sizeof( addon_Tracer_data_t )
// 	};
		addon_coclass_desc_t __coclass_desc;
		
		__coclass_desc.vtbl_size	= (unsigned long)sizeof(__coclass_vtbl);
		__coclass_desc.vtbl_funcs	= (unsigned char *)__coclass_vtbl;
		__coclass_desc.priv_size	= (unsigned long)sizeof(addon_Tracer_data_t);
		


	return ( ADDON_REF(IObject) )addon_obj_new( (addon_coclass_desc_ptr_t)&__coclass_desc, reserved );
}

#else	// #if (__ADDON_TARGET__ == __TARGET_SYMBIAN)

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
ADDON_EXPORT ADDON_REF(IObject) addon_create_tracer( void * reserved )
{
	ADDON_UNUSED( reserved );
	return NULL;
}

#endif	// #if (__ADDON_TARGET__ == __TARGET_SYMBIAN)
